#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include "Util.hpp"
// 网络版本计算器定制协议
namespace vic_protocol
{

#define SEP " "
#define SEP_LEN strlen(SEP)
#define HEADER_SEP "\r\n"
#define HEADER_SEP_LEN strlen("\r\n")

    // 发送的报文： 长度\r\n""10 + 20"\r\n
    //             报头+有效载荷
    std::string AddHeader(const std::string &str)
    {
        std::string s = std::to_string(str.size());
        s += HEADER_SEP;
        s += str;
        s += HEADER_SEP;
        return s;
    }
    std::string RemoveHeader(const std::string &str,int len)
    {
        return str.substr(str.size()-HEADER_SEP_LEN-len,len);
    }
    int ReadPackage(int sock, std::string &inbuffer, std::string *package)
        {
            // 边读取
            char buffer[1024];
            ssize_t s = recv(sock, buffer, sizeof(buffer) - 1, 0);
            if (s <= 0)
            {
                return -1;
            }
            buffer[s] = 0;
            inbuffer += buffer;
            // 边分析 "7"\rn"10 + 20"\r\n
            auto pos = inbuffer.find(HEADER_SEP);
            if (pos == std::string::npos)  return 0;             // inbuffer里保存历史数据
            std::string lenStr = inbuffer.substr(0, pos); // 获取了头部字符串（长度）
            int len = Util::toInt(lenStr);
            // 完整报文长度
            int targetpackageLen = lenStr.size() + HEADER_SEP_LEN + len + HEADER_SEP_LEN;
            if (inbuffer.size() < targetpackageLen) return 0;
            *package=inbuffer.substr(pos+HEADER_SEP_LEN,len);
            inbuffer.erase(0,targetpackageLen);//从i你buffer中直接移除整个报文
            return len;
        }
    // request和respones都要提供序列化和反序列化功能
    class Request
    {
    public:
        Request() {}
        Request(int x, int y, char op) : _x(x), _y(y), _op(op) {}
        ~Request() {}
        // 序列化 struct-》string
        bool Serialize(std::string *outStr)
        {
            *outStr = "";
            //_x op _y
            std::string x_string = std::to_string(_x);
            std::string y_string = std::to_string(_y);
            // 手动序列化
            *outStr = x_string + SEP + _op + SEP + y_string;
            return true;
        }
        // 反序列化string->struct
        bool DeSerialize(const std::string &inStr)
        {
            // instr:10 + 20
            // string->vector [0]:10 [1]:+ [2]:20
            std::vector<std::string> result;
            Util::StringSplit(inStr, SEP, &result);
            if (result.size() != 3)
                return false;
            _x = Util::toInt(result[0]);
            _y = Util::toInt(result[2]);
            if (result[1].size() != 1)
                return false;
            _x = result[1][0];
            return true;
        }
       
        int _x;
        int _y;
        char _op;
    };
    class Response
    {
    public:
        Response() {}
        Response(int reslut, int code) : _result(reslut), _code(code) {}

        ~Response() {}
        bool Serialize(std::string *outStr)
        {
            *outStr = "";

            std::string res_string = std::to_string(_result);
            std::string code_string = std::to_string(_code);
            // 手动序列化
            *outStr = res_string + SEP + code_string;
            return true;
        }
        bool DeSerialize(const std::string &inStr)
        {

            std::vector<std::string> result;
            Util::StringSplit(inStr, SEP, &result);
            if (result.size() != 2)
                return false;
            _result = Util::toInt(result[0]);
            _code = Util::toInt(result[1]);

            return true;
        }
        int _result;
        int _code; // 0表示可计算
    };
}